package ${package_name}.service;
import java.util.Date;
import com.wx.commons.tools.PageTool;
import ${package_name}.po.${table_name}PO;
import ${package_name}.dao.${table_name}Dao;
import ${package_name}.dto.${table_name}DTO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 描述：${table_name} 服务实现层
* @author ${author}
* @date ${date}
*/
@Service
public class ${table_name}Service {

    @Autowired
    private ${table_name}Dao ${table_name?uncap_first}dao;

    /**
     * 保存数据
     *
     * @author: ${author}
     * @date: ${date}
     * @param ${table_name}DTO 传输对象
     */
    public void save(${table_name}DTO ${table_name?uncap_first}DTO) {
        ${table_name}PO ${table_name?uncap_first}PO = new ${table_name}PO();
        BeanUtils.copyProperties(${table_name?uncap_first}TO, ${table_name?uncap_first}PO);
        ${table_name?uncap_first}PO.setId(IdUtils.getId());
        ${table_name?uncap_first}PO.setCreateTime(new Date());
        ${table_name?uncap_first}PO.setCreatorId(BusinessContextUtils.get().getUserId());
        ${table_name?uncap_first}dao.save(${table_name?uncap_first}PO);
    }

    /**
     * 根据ID删除
     *
     * @author: ${author}
     * @date: ${date}
     * @param id 数据ID
     */
    public void deleteById(String id) {
    	if (StringUtils.isBlank(id)) {
    		return;
    	}
        ${table_name?uncap_first}dao.deleteById(id);
    }

    /**
     * 根据ID修改
     *
     * @author: ${author}
     * @date: ${date}
     * @param ${table_name}TO 传输对象
     */
	public void updateById(${table_name}TO ${table_name?uncap_first}TO) {
        ${table_name}PO ${table_name?uncap_first}POResult = ${table_name?uncap_first}dao.getById(${table_name?uncap_first}TO.getId());
        if (${table_name?uncap_first}POResult == null) {
            // TODO 抛出业务异常
        }
        ${table_name}PO ${table_name?uncap_first}PO = new ${table_name}PO();
        BeanUtils.copyProperties(${table_name?uncap_first}TO, ${table_name?uncap_first}PO);
        ${table_name?uncap_first}PO.setModifierId(BusinessContextUtils.get().getUserId());
        ${table_name?uncap_first}PO.setModifyTime(new Date());
        ${table_name?uncap_first}dao.updateById(${table_name?uncap_first}PO);
	}

    /**
     * 根据id查询
     *
     * @author: ${author}
     * @date: ${date}
     * @param id 主键编号
     * @return ${table_name}VO
     */
	public ${table_name}VO getById(String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}

        ${table_name}PO ${table_name?uncap_first}PO = ${table_name?uncap_first}dao.getById(id);
		if (${table_name?uncap_first}PO == null) {
			return null;
		}

        ${table_name}VO ${table_name?uncap_first}VO = new ${table_name}VO();
		BeanUtils.copyProperties(${table_name?uncap_first}PO, ${table_name?uncap_first}VO);

		return ${table_name?uncap_first}VO;
	}

    /**
     * 根据条件查询
     *
     * @author: ${author}
     * @date: ${date}
     * @param ${table_name}QO 查询条件
     * @return ${table_name}VO
     */
	public ${table_name}VO getByCondition(${table_name}QO ${table_name?uncap_first}QO) {
		if (${table_name?uncap_first}QO == null) {
			return null;
		}

        ${table_name}PO ${table_name?uncap_first}PO = ${table_name?uncap_first}dao.getByCondition(${table_name?uncap_first}QO);
		if (${table_name?uncap_first}PO == null) {
			return null;
		}

        ${table_name}VO ${table_name?uncap_first}VO = new ${table_name}VO();
		BeanUtils.copyProperties(${table_name?uncap_first}PO, ${table_name?uncap_first}VO);

		return ${table_name?uncap_first}VO;
	}

    /**
     * 根据条件查询集合
     *
     * @author: ${author}
     * @date: ${date}
     * @param ${table_name}QO 查询对象
     * @return List<${table_name}VO>
    */
    public List<${table_name}VO> queryList(${table_name}QO ${table_name?uncap_first}QO) {
        if (${table_name?uncap_first}QO == null) {
             return null;
        }

        List<${table_name}PO> ${table_name?uncap_first}POList = ${table_name?uncap_first}dao.queryList(${table_name?uncap_first}QO);
        if (CollectionUtils.isEmpty(${table_name?uncap_first}POList)) {
            return null;
        }
        List<${table_name}VO> ${table_name?uncap_first}VOList = new ArrayList<>();
        for (${table_name}PO ${table_name?uncap_first}PO : ${table_name?uncap_first}POList) {
            ${table_name}VO ${table_name?uncap_first}VO = new ${table_name}VO();
            BeanUtils.copyProperties(${table_name?uncap_first}PO, ${table_name?uncap_first}VO);
            ${table_name?uncap_first}VOList.add(${table_name?uncap_first}VO);
        }

        return ${table_name?uncap_first}VOList;
    }



    /**
    * 根据条件分页查询集合
    *
    * @author: ${author}
    * @date: ${date}
    * @param ${table_name}QO 查询对象
    * @return List<${table_name}VO>
    */
    public PageTool<${table_name}VO> queryPage(${table_name}QO ${table_name?uncap_first}QO) {
        if (${table_name?uncap_first}QO == null) {
            return null;
        }

        List<${table_name}VO> ${table_name?uncap_first}VOList = null;
        List<${table_name}PO> ${table_name?uncap_first}POList = ${table_name?uncap_first}dao.queryPage(${table_name?uncap_first}QO);
            if (CollectionUtils.isNotEmpty(${table_name?uncap_first}POList)) {
                ${table_name?uncap_first}VOList = new ArrayList<>();
                for (${table_name}PO ${table_name?uncap_first}PO : ${table_name?uncap_first}POList) {
                    ${table_name}VO ${table_name?uncap_first}VO = new ${table_name}VO();
                    BeanUtils.copyProperties(${table_name?uncap_first}PO, ${table_name?uncap_first}VO);
                    ${table_name?uncap_first}VOList.add(${table_name?uncap_first}VO);
                }
            }

            long recordCount = ${table_name?uncap_first}dao.countQueryPage(${table_name?uncap_first}QO);

            PageTool<${table_name}VO> page = new PageTool<${table_name}VO>();
            page.setPageNum(${table_name?uncap_first}QO.getPageNum());
            page.setPageSize(${table_name?uncap_first}QO.getPageSize());
            page.setRecordCount(recordCount);
            page.setList(${table_name?uncap_first}VOList);
            return page;
    }
}